#region File Header
//----------------------------------------------------------------
// Copyright (C) 2002 Stefan Kyntchev
// All rights reserved.
//
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY 
// OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT 
// LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR 
// FITNESS FOR A PARTICULAR PURPOSE.
//----------------------------------------------------------------
#endregion File Header

namespace FlashBack.Web
{
    using System;
	using AirWeb.WebControls.TreeControl;
	using System.IO;
	using System.Collections;
	using System.Web;
	using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;
	using AirWeb.DirectoryMgr;
	using AirWeb.Security;
	using AirWeb.ApplicationTools;

	public class DirectoryItem : NodeItem
	{
		private string m_Directory;
		private string m_Name;
		private bool m_New;
		private bool m_Public;
		private string m_FullPath;

		public override void Render(ControlCollection i_context)
		{
			// Add a separator
			Label l = new Label();
			l.Text = " ";
			i_context.Add(l);

			// Add the link
			HtmlGenericControl a = new HtmlGenericControl("a");
			
			// Handle Windows CE differently
			HttpBrowserCapabilities c = HttpContext.Current.Request.Browser;
			if(c.Platform != "WinCE")
			{
				m_Directory = m_Directory.Replace("'","%%"); // escape ' with %%;
				a.Attributes["href"] = "javascript:top.OnNewPath('" + m_Directory + "')";
			}
			else
			{
				a.Attributes["href"] = "ThumbNails.aspx?path=" + m_Directory;
			}

			a.InnerText = " " + m_Name;
			a.Attributes["class"] = "fakeAnchor";
			a.Attributes["title"] = m_Name;

			Image i = new Image();

			i_context.Add(l);

			if(m_Directory.IndexOf("Movie") != -1)
			{
				i.ImageUrl = "images/movie.gif";
			}
			else if(IsPublic)
			{
				i.ImageUrl = "images/bo.gif";
			}
			else if(Owner.Expanded)
			{
				i.ImageUrl = "images/Camera.gif";
			}
			else
			{
				i.ImageUrl = "images/Camera.gif";
			}
			
			if(IsNew)
			{
				Image n = new Image();
				n.ImageUrl = "images/new.gif";
				n.ToolTip = "New pictures added this week";
				i_context.Add(n);
			}

			i_context.Add(i);
			i_context.Add(a);
		}

		public string DirectoryPath
		{
			get
			{
				return m_Directory;
			}
			set
			{
				m_Directory = value;
				m_Directory = m_Directory.Replace('\\','/');
			}
		}

		public string Name
		{
			get
			{
				return m_Name;
			}
			set
			{
				m_Name = value;
			}
		}

		public bool IsNew
		{
			get
			{
				return m_New;
			}
			set
			{
				m_New = value;
			}
		}

		public bool IsPublic
		{
			get
			{
				return m_Public;
			}
			set
			{
				m_Public = value;
			}
		}

		public string FullDirectoryPath
		{
			get { return m_FullPath; }
			set { m_FullPath = value; }
		}
	}

    /// <summary>
    ///    .
    /// </summary>
	public class DirectoryTreeModel : ITreeControlModel
	{
		private string path = "";
		private int m_filter = 0;
		private HttpContext m_context;
		private DirectoryLister m_Lister;
		private SecurityFileFilter m_secFilter;
		private User m_User;

		public DirectoryTreeModel(User i_user, HttpContext i_Context)
		{
			m_User = i_user;
			m_context = i_Context;
		}

		public void BuildModel ( AirWeb.WebControls.TreeControl.TreeNode l ) 
		{
			// Build our directory lister
			m_Lister = new DirectoryLister();
			m_Lister.Comparer = new FileComparer(CompareBy.CreationDate,SortOrder.Ascending);

			m_secFilter = new SecurityFileFilter(m_User,m_context);
			m_Lister.AddFilter(new IncludeFilter(m_secFilter.SecurityFileFilterInclude));
			
			if(m_filter > 0)
			{
				DateFilter df = new DateFilter(m_filter);
				m_Lister.AddFilter(new IncludeFilter(df.DateFilterInclude));
			}

			// Bild the model
			DirectoryInfo dir = new DirectoryInfo(path);

			if(!dir.Exists)
			{
				return;
			}

			DirectoryItem di = new DirectoryItem();
			di.FullDirectoryPath = path;
			l.nodeItem = di;

			AddChildNodes(l);
		}

		public void SetPath( string p )
		{
			path = p;
		}

		public void SetFilter( int f )
		{
			m_filter = f;
		}

		public void AddChildNodes(AirWeb.WebControls.TreeControl.TreeNode l)
		{
			if(!l.Expanded)
				return;

			DirectoryItem diritem = (DirectoryItem)l.nodeItem;
			ArrayList dl = new ArrayList(m_Lister.ListDirectories(diritem.FullDirectoryPath,0));

			if(dl.Count != 0)
			{
				for(int a= 0; a != dl.Count; a++ )
				{
					DirectoryInfo d = (DirectoryInfo)dl[a];

					String dirname = d.Name;
					String content = "";
					DirectoryItem di = new DirectoryItem();

					// Set the "Public" folder at the end of the list
					if(dirname == "Public")
					{
						if( a != dl.Count-1)
						{
							dl.RemoveAt(a);
							dl.Add(d);
							--a;
							continue;
						}
						else
						{
							di.IsPublic = true;
						}
					}

					// Put the number of pictures only if the user has access to the folder
					// Users may not have access to some folders but they should be displayed
					// if they have children that the user has access to
					bool inc = true;
					bool hasChildren = false;
					m_secFilter.SecurityFileFilterInclude((FileSystemInfo)dl[a],null,ref inc);
					if(inc == true)
					{
						int numfiles =  d.GetFiles().Length;
						int numdirs =  d.GetDirectories().Length;

						if(numdirs > 0)
							hasChildren = true;

						if(numfiles == 0 && numdirs == 0)
						{
							content = " ( No Pictures )";
						}

						if( numfiles != 0)
						{
							if(dirname.IndexOf("Movies") != -1)
							{
								content = " (" + numfiles + " Movies" + ")";	
							}
							else
							{
								content = " (" + numfiles + " Pictures" + ")";	
							}
						}
					}
					
					dirname += content;
					
					String fullname = d.FullName;

					di.DirectoryPath = fullname.Substring(path.Length, fullname.Length-path.Length);

					di.Name = dirname;
					di.FullDirectoryPath = fullname;

					DateTime lastweek = DateTime.Now;
					lastweek = lastweek.AddDays(-7);

					if(d.LastWriteTime > lastweek)
					{
						di.IsNew = true;
					}

					AirWeb.WebControls.TreeControl.TreeNode child = new AirWeb.WebControls.TreeControl.TreeNode(l);
					child.nodeItem = di;
					child.HasChildren = hasChildren;
					 

					l.AddChild(child);
					AddChildNodes(child);
				}				
			}
		}
	}
}
